package task3;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class FileCopyTest {
    private ExecutorService pool;
    private String original;
    private String destination;

    public FileCopyTest(int nThreads, String original, String destination) {
        pool =  Executors.newFixedThreadPool(nThreads);
        this.original = original;
        this.destination = destination;
    }

    public ExecutorService getPool() {
        return pool;
    }

    public void setPool(ExecutorService pool) {
        this.pool = pool;
    }

    public String getOriginal() {
        return original;
    }

    public void setOriginal(String original) {
        this.original = original;
    }

    public String getDestination() {
        return destination;
    }

    public void setDestination(String destination) {
        this.destination = destination;
    }

    public void loop(File file){
        if(file.isDirectory()){
            File destD = new File(file.getAbsolutePath().replace(original, destination));
            System.out.println("[" + file.getAbsolutePath() + "]为文件夹，" + "创建目标文件夹：" + destD.getAbsolutePath());
            destD.mkdirs();
            File[] files = file.listFiles();
            for(File f: files){
                loop(f);
            }
        } else{
            CopyThread ct = new CopyThread(file, original, destination);
            pool.submit(ct);
        }
    }

    public void shutdown(){
        pool.shutdown();
    }

    public static void main(String[] args) {
        FileCopyTest fct = new FileCopyTest(5, "d:\\a", "d:\\b");
        fct.loop(new File(fct.getOriginal()));
        fct.shutdown();
    }
}
